<?php
/**
 * 用户管理控制器
 */
namespace Home\Controller;
use Think\Controller;
class YhglController extends AController {
    public function _empty(){
        $this->redirect('Yhgl/index');
    }
    //用户管理首页,创建用户信息
    public function index(){
        if(IS_POST){
            $pic = I('pic');
            $name= I('name');
            $nickname=I('nickname');
            $phone=I('phone');
            $email = I('email');
            $sex = I('sex');
            $pass = I('pass');
            if(!$name && !$phone && !$email){
                $arr = array(
                    'code'=>0,
                    'msg'=>L('USER_ERROR_1')
                );
                $this->ajaxReturn($arr);
            }
            if(!$pass){
                $arr = array(
                    'code'=>0,
                    'msg'=>L('USER_ERROR_2')
                );
                $this->ajaxReturn($arr);
            }
            $data = array(
                'head_pic'=>$pic,
                'name'  =>$name,
                'nickname'=>$nickname,
                'phone'=>$phone,
                'email'=>$email,
                'sex'=>$sex,
                'password'=>$pass,
            );
            $res = M('user')->add($data);
            if($res){
                $arr = array(
                    'code'=>1,
                    'msg'=>L('_SUCCESS')
                );
            }else{
                $arr = array(
                    'code'=>0,
                    'msg'=>L('_FAIL')
                );
            }
            $this->ajaxReturn($arr);
        }else{
            $this->display();
        }
    }
    //删除用户信息
    public function del(){
        $id = I('id');
        $res = M('user')->delete($id);
        if($res){
            $arr = array(
                'code'=>1,
                'msg'=>L('_SUCCESS')
            );
        }else{
            $arr = array(
                'code'=>0,
                'msg'=>L('_FAIL')
            );
        }
        $this->ajaxReturn($arr);
    }

    //修改用户信息
    public function edit(){
        if(IS_POST){
            $id =I('id');
            //判断id是否正确
            if(!$id){
                $arr = array(
                    'code'=>0,
                    'msg'=>L('_PARAMETER_ERROR')
                );
                $this->ajaxReturn($arr);
            }
            $pic = I('pic');
            $name= I('name');
            $nickname=I('nickname');
            $phone=I('phone');
            $email = I('email');
            $sex = I('sex');
            $pass = I('pass');
            if(!$name && !$phone && !$email){
                $arr = array(
                    'code'=>0,
                    'msg'=>L('USER_ERROR_1')
                );
                $this->ajaxReturn($arr);
            }
            if(!$pass){
                $arr = array(
                    'code'=>0,
                    'msg'=>L('USER_ERROR_2')
                );
                $this->ajaxReturn($arr);
            }
            $data = array(
                'id'=>$id,
                'head_pic'=>$pic,
                'name'  =>$name,
                'nickname'=>$nickname,
                'phone'=>$phone,
                'email'=>$email,
                'sex'=>$sex,
                'password'=>$pass,
            );
            $res = M('user')->save($data);
            if($res){
                $arr = array(
                    'code'=>1,
                    'msg'=>L('_SUCCESS')
                );
            }else{
                $arr = array(
                    'code'=>0,
                    'msg'=>L('_FAIL')
                );
            }
            $this->ajaxReturn($arr);
        }else{
            $id = I('id');
            $user = M('user')->where('id='.$id)->find();
            $this->assign('user',$user);
            $this->display();
        }
    }
    //获取用户信息
    public function info(){
        $key_word = I('get.key_word');

        $p = I('p');
        if(!$p){
            $p = 1;
        }
        $num = 10;

        if($key_word){
            $word='%'.$key_word.'%';
            $where['id']=array('eq',$key_word);
            $where['name']=array('like',$word);
            $where['nickname']=array('like',$word);
            $where['email']=array('like',$word);
            $where['phone']=array('eq',$key_word);
            $where['_logic'] = 'OR';
            $list = M('user')->where($where)->order('id desc')->page($p.','.$num)->select();
            //print_r($list);
            $count= M('user')->where($where)->count();
            // echo  M('user')->getLastSql();
            //echo $count;
            //$this->ajaxReturn($list);
        }else{
            //查询用户
            $list = M('user')->order('id desc')->page($p.','.$num)->select();
            $count      = M('user')->count();// 查询满足要求的总记录数
        }

        import("ORG.Util.Page");// 导入分页类
        $Page = getpage($count,$num);
        if(LANG_SET != 'zh-cn'){
            $Page->setConfig('header','Total&nbsp;&nbsp;'.$count.'&nbsp;&nbsp;Record');
            $Page->setConfig('first','first');
            $Page->setConfig('prev','<<');
            $Page->setConfig('next','>>');
            $Page->setConfig('last','last');
        }
        $show       = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('list',$list);
        $this->display();
    }
}
